
var name = ''
var price = 0
var num = 0
utils.on( tbody, 'click', function (e) {
    var target = e.target
    if(Array.from(target.classList).includes('btn-del')) {
        console.log('del')
        if(confirm('确定删除该商品吗？')) {
            const tr = target.parentNode.parentNode
            const id = tr.getAttribute('data-id')
            utils.fetch("./api/shop/delete.php" , { id }).then (resp => {
    
                if(resp.code === 200) {
                    getLIst()
                }
            } 
            )
        }
        
    }
    if(Array.from(target.classList).includes('btn-edit')) {
        const td = target.parentNode
        const tr = td.parentNode
        
        const id = Number(tr.getAttribute('data-id'))
        console.log(id)
        tr.classList.add('edit')
    var shopName = tr.querySelector('#edit-shop-name'),
        shopPrice = tr.querySelector('#edit-shop-price'),
        shopNum = tr.querySelector('#edit-shop-num')
    var spanName = tr.querySelector('#edit-span-name'),
        spanPrice = tr.querySelector('#edit-span-price'),
        spanNum = tr.querySelector('#edit-span-num')

        shopName.value = spanName.innerHTML
        shopPrice.value = spanPrice.innerHTML
        shopNum.value = spanNum.innerHTML 
        
    }
    if(Array.from(target.classList).includes('btn-ok')) {
        const td = target.parentNode
        const tr = td.parentNode
        const id = Number(tr.getAttribute('data-id'))
        console.log(id)
        tr.classList.add('edit')
        var shopName = tr.querySelector('#edit-shop-name'),
            shopPrice = tr.querySelector('#edit-shop-price'),
            shopNum = tr.querySelector('#edit-shop-num')
        name = shopName.value
        price = shopPrice.value
        num = shopNum.value
        utils.fetch('./api/shop/edit.php', {id, name, price, num}).then(resp => {
                    console.log(resp)
                    if(resp.code === 200) {
                        getLIst()
                        tr.classList.add('edit')
                    }
              })

    }

    if(Array.from(target.classList).includes('btn-cancel')) {
        const td = target.parentNode
        const tr = td.parentNode
            
        tr.classList.remove('edit') 
    }
})